package current.limiting.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;

/**
 * 系统相关配置
 *
 * @author LiKun
 * @date 2021/10/25 15:24
 */
@Configuration
public class ApplicationConfig {
    @Resource
    private RedisConnectionFactory redisConnectionFactory;

    /**
     * Redis序列化相关配置
     *
     * @return RedisTemplate
     */
    @Bean
    public RedisTemplate redisTemplate() {
        RedisTemplate<Object, Object> template = new RedisTemplate();
        StringRedisSerializer serializer = new StringRedisSerializer();
        GenericToStringSerializer<Object> generic = new GenericToStringSerializer(Object.class);

        template.setValueSerializer(generic);
        template.setKeySerializer(serializer);
        template.setHashValueSerializer(generic);
        template.setHashKeySerializer(serializer);
        template.setConnectionFactory(redisConnectionFactory);

        return template;
    }
}
